package cn.iocoder.yudao.module.fengdong.smartelderly.controller.app.commodity;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.commodity.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.commodity.CommodityConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.commodity.CommodityDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.commodity.CommodityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "用户 APP - 商品")
@RestController
@RequestMapping("/fengdong.smartelderly/commodity")
@Validated
public class AppCommodityController {

    @Resource
    private CommodityService commodityService;

    @PostMapping("/create")
    @Operation(summary = "创建商品")
    public CommonResult<Long> createCommodity(@Valid @RequestBody CommodityCreateReqVO createReqVO) {
        return success(commodityService.createCommodity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品")
    public CommonResult<Boolean> updateCommodity(@Valid @RequestBody CommodityUpdateReqVO updateReqVO) {
        commodityService.updateCommodity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCommodity(@RequestParam("id") Long id) {
        commodityService.deleteCommodity(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CommodityRespVO> getCommodity(@RequestParam("id") Long id) {
        CommodityDO commodity = commodityService.getCommodity(id);
        return success(CommodityConvert.INSTANCE.convert(commodity));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商品列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<CommodityRespVO>> getCommodityList(@RequestParam("ids") Collection<Long> ids) {
        List<CommodityDO> list = commodityService.getCommodityList(ids);
        return success(CommodityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品分页")
    public CommonResult<PageResult<CommodityRespVO>> getCommodityPage(@Valid CommodityPageReqVO pageVO) {
        PageResult<CommodityDO> pageResult = commodityService.getCommodityPage(pageVO);
        return success(CommodityConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/querypage")
    @Operation(summary = "获得购买记录分页")
    public CommonResult<IPage<CommodityRespIpageVO>> getCommodityIPage(@Valid CommodityIPageReqVO pageVO) {
        IPage<CommodityRespIpageVO> pageResult = commodityService.getCommodityIPage(pageVO);
        return success(pageResult);
    }

}
